function makeRunData_acctCreation_ctsTime(runId)

addpath('../general_funcs');

config = getConfig_acctCreation(runId);

dataId = config.dataId;
spatialMatricesFolder = config.spatialMatricesFolder;

runDataFile           = config.runDataFile;

%%%%% Get covariates (including FEs)
covariates = config.covariates;
spatialMatrices = config.spatialMatrices;

myX_tk_names   = covariates.myX_tk_names;
myX_tk_transfs = covariates.myX_tk_transfs;

myZ_names	= covariates.myZ_names;
myZ_transfs	= covariates.myZ_transfs;

myAnames	= covariates.myAnames;
myAtransfs	= covariates.myAtransfs;

myZ_geo_FE_Names  = covariates.myZ_geo_FE_Names;
time_FE_Names = covariates.time_FE_Names;

%%% Load files
data0 = loadDataFilesCtsTime_acctCreation(dataId);
ctsTimeData  = data0.ctsTimeData;
periodicData = data0.periodicData;

%%%%%%%%%%%%%%%%%% LOAD STUFF FROM ctsTimeData %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewInactiveUsers    = ctsTimeData.NewInactiveUsers;    % NumSpells x K
NewConsumers    = ctsTimeData.NewConsumers;    % NumSpells x K
NewProviders     = ctsTimeData.NewProviders;     % NumSpells x K
DeletedProviders = ctsTimeData.DeletedProviders; % NumSpells x K
laggedConsumers = ctsTimeData.laggedConsumers;     % NumSpells x K - sparse
laggedProviders  = ctsTimeData.laggedProviders;      % NumSpells x K  - sparse
laggedInactiveUsers = ctsTimeData.laggedInactiveUsers; % NumSpells x K  - sparse

spellDurations    = ctsTimeData.spellDurations;    % NumSpells x 1
spell2period      = ctsTimeData.spell2period;      % NumSpells x 1 (integer between 1 and NumPeriods)

%%%%%%%%%%%%%%%%%% LOAD STUFF FROM periodicData %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
population        = periodicData.population;        % K x 1
surfaces          = periodicData.surfaces;          % K x 1
incomingCommuters = periodicData.incomingCommuters; % K x 1
touristBeds       = periodicData.touristBeds;       % K x 1
periodLabels = periodicData.periodLabels;
geoLabels   = periodicData.geoLabels;
A     = periodicData.A;
Z     = periodicData.Z;
X     = periodicData.X;
Anames     = periodicData.Anames;
Znames     = periodicData.Znames;
Xnames     = periodicData.Xnames;
surfaceName         = periodicData.surfaceName;
geoAggLevelNames    = periodicData.geoAggLevelNames;
geoAggLevelLabels   = periodicData.geoAggLevelLabels;
timeAggLevelNames   = periodicData.timeAggLevelNames;
timeAggLevelLabels = periodicData.timeAggLevelLabels;

% Read dimensions
NumSpells = size(NewProviders, 1);
NumPeriods = size(A,1);
K = size(Z,1);


%%% Make Y (=NewProviders)
if config.jj_idx == 1
	Y = NewInactiveUsers;  % NumSpells x K (sparse)
	Yname = 'AcctCreationInactives';
	marketSize = Z(:,find(strcmp(Znames, 'NumPersons19orOlder'))); % K x 1
	initialSeed = laggedConsumers(1,:)'; % K x 1
	initialSeedName = 'Consumers';
	popAtRisk = marketSize' - laggedConsumers - laggedInactiveUsers; % NumSpells x K
	
else if config.jj_idx == 2
	Y = NewConsumers;  % NumSpells x K (sparse)
	Yname = 'AcctCreationConsumers';
	marketSize = Z(:,find(strcmp(Znames, 'NumPersons19orOlder'))); % K x 1
	initialSeed = laggedConsumers(1,:)'; % K x 1
	initialSeedName = 'Consumers';
	popAtRisk = marketSize' - laggedConsumers - laggedInactiveUsers; % NumSpells x K
	
else if config.jj_idx == 3
	Y = NewProviders;  % NumSpells x K (sparse)
	Yname = 'AcctCreationProviders';
	marketSize = Z(:,find(strcmp(Znames, 'NumHHsWithCar'))); % K x 1
	initialSeed = laggedProviders(1,:)'; % K x 1
	initialSeedName = 'Providers';
	popAtRisk = marketSize' - laggedProviders; % NumSpells x K
else if config.jj_idx == 4
	Y = DeletedProviders;  % NumSpells x K (sparse)
	Yname = 'ProviderExits';
else
	error('Wrong jj_idx.');
end; end; end; end;
clear NewConsumers NewProviders DeletedProviders;

%%% Make logLambdaOffset and popAtRisk
logLambdaOffset = log(popAtRisk); % NumSpells x K

% Reshape Y, installed bases and logLambdaOffset
Y               = reshape(Y,                 [NumSpells*K  1]); % (NumSpells*K) x 1
laggedProviders    = reshape(laggedProviders,      [NumSpells*K 1]); % (NumSpells*K x 1)
laggedConsumers   = reshape(laggedConsumers, [NumSpells*K 1]); % (NumSpells*K x 1)
logLambdaOffset = reshape(logLambdaOffset,   [NumSpells*K, 1]); % (NumSpells*K) x 1


%%%%% Load spatial matrices
geoLabels0 = geoLabels;
NumSpatialMatrices = length(spatialMatrices.names);
Ws     = cell(1,NumSpatialMatrices);
Wnames = spatialMatrices.names;
for ss = 1:NumSpatialMatrices
	matName = spatialMatrices.names{ss};
	if strcmp(matName, 'sameCanton')
		W = eye(K); % K x K
	else
		load(sprintf('%s/%s.mat', spatialMatricesFolder, matName), 'W', 'geoLabels');
		if exist('geoLabels')
			if ~isequal(geoLabels, geoLabels0); error('Inconsistency in geoLabels!'); end;
			clear geoLabels;
		end
		W = full(W); % This may be needed for sparse matrices (e.g. contiguity matrix).
		if any(isnan(W(:)))
			warning(sprintf('Matrix %s has NaNs --> they are replaced by zeros.', matName));
			W(isnan(W)) = 0; % This is because Weight_TouristTripCondDestination has NaN's (some cantons with no tourist bed)
		end
	end
	Ws{ss} = W;
	clear W matName;	
end
geoLabels = geoLabels0; clear geoLabels0;



% Get myX_tk and myX_tk_names
[Xflag, X_idces] = ismember(myX_tk_names, Xnames);
if any(~Xflag); error('Could not find all Xnames.'); end;
if any(~strcmp(Xnames(X_idces), myX_tk_names)); error('Mistmatch'); end;
myX_tk         = X(:,:, X_idces);				% NumPeriods x K x NumX
myX_tk         = reshape(myX_tk, NumPeriods*K, size(myX_tk,3));	% (NumPeriods*K) x NumX
[myX_tk, myX_tk_names] = transformMatrix(myX_tk, myX_tk_transfs, myX_tk_names, reshape(repmat(surfaces', NumPeriods, 1), NumPeriods*K, 1),surfaceName);
	% (NumPeriods*K) x NumX and cell(1,NumX) of strings

% Get myZ and myZ_names
[Zflag, Z_idces] = ismember(myZ_names, Znames);
if any(~Zflag); error('Could not find all Znames.'); end;
if any(~strcmp(Znames(Z_idces), myZ_names)); error('Mistmatch'); end;
myZ         = Z(:, Z_idces);				% K x NumZ
[myZ, myZ_names] = transformMatrix(myZ, myZ_transfs, myZ_names, surfaces, surfaceName);
	% K x NumZ and cell(1,NumZ) of strings

% Get myA and myAnames
[Aflag, A_idces] = ismember(myAnames, Anames);
if any(~Aflag); error('Could not find all Anames.'); end;
if any(~strcmp(Anames(A_idces), myAnames)); error('Mistmatch'); end;
myA         = A(:, A_idces);				% NumPeriods x NumA
	% NumPeriods x NumA and cell(1,NumA) of strings

% Add intercept to myA
myA = [ones(NumPeriods,1) myA];
myAnames = [{'Intercept'} myAnames];
myAtransfs = [-1 myAtransfs];


%% Make time_FEs
[flag, idxes] = ismember(time_FE_Names, Anames);
if any(~flag); error('Could not find all time FE names.'); end;
time_FEs = A(:,idxes);
Num_time_FE_sets = length(time_FE_Names);
Num_time_FE_vals = zeros(1,Num_time_FE_sets);
time_FE_labels = cell(1,Num_time_FE_sets);
for aa = 1:Num_time_FE_sets
	time_FEs_aa = time_FEs(:,aa);
	time_FE_Name_aa = time_FE_Names{aa};
	
	unq_time_FEs_aa = sort(unique(time_FEs_aa));
	time_FE_labels_aa = strcat(time_FE_Name_aa, sprintfc('_is_%d', unq_time_FEs_aa));
	[flag,newTime_FEs_aa] = ismember(time_FEs_aa, unq_time_FEs_aa);
	
	time_FEs(:,aa) = newTime_FEs_aa;
	Num_time_FE_vals(aa) = length(unq_time_FEs_aa);
	time_FE_labels{aa} = time_FE_labels_aa;
end

%% Make  myZ_geo_FEs + get myZ_geo_FE_labels +  myZ_Num_geo_FE_vals
[flag, idxes] = ismember(myZ_geo_FE_Names, Znames);
if any(~flag); error('Could not find all geo FE names.'); end;
myZ_geo_FEs = Z(:,idxes);
Num_geo_FE_sets = length(myZ_geo_FE_Names);
myZ_Num_geo_FE_vals = zeros(1,Num_geo_FE_sets);
myZ_geo_FE_labels = cell(1,Num_geo_FE_sets);
for aa = 1:Num_geo_FE_sets
	geo_FE_Name_aa = myZ_geo_FE_Names{aa};
	[flag,geoidx] = ismember(geo_FE_Name_aa, geoAggLevelNames);
	if ~flag; error('Could not find geo_FE_name among geoAggLevelNames'); end;
	geo_FE_labels_aa = geoAggLevelLabels{geoidx};
	myZ_Num_geo_FE_vals(aa) = length(geo_FE_labels_aa);
	myZ_geo_FE_labels{aa} = geo_FE_labels_aa;
end


%%% Add treatment for initial installed base %%%
if config.initialBaseTreatment
	[myZ, myZ_names] = addInitialBaseCovariate(initialSeed, myZ, myZ_names, initialSeedName);
end


%%%% Make Xdynamic_staticInputs and Xparts_dynamic (Xparts that depend on installed base)
myX_tk = reshape(myX_tk, [NumPeriods, K, size(myX_tk,2)]);	% NumPeriods x K x NumX
myX_tk = myX_tk(spell2period,:,:); % NumSpells x K x NumX
myX_tk = reshape(myX_tk, [NumSpells*K, size(myX_tk,3)]);	% (NumSpells*K) x NumX

Xparts_static{1}.X      = myX_tk;             % (NumSpells*K) x NumXstatic
Xparts_static{1}.Xnames = myX_tk_names;       % cell(1,NumXstatic) of strings
Xparts_static{1}.X_FEs        = zeros(NumSpells*K,0); % (NumSpells*K) x NumX_FEs
Xparts_static{1}.X_FE_labels  = {};           % cell(1, NumX_FEs)
Xparts_static{1}.NumX_FE_vals = zeros(1,0);   % 1 x NumX_FEs

Xdynamic_staticInputs.Xparts_static = Xparts_static;
if config.laggedBaseTransf.code == 1 || config.laggedBaseTransf.code == 3
	Xdynamic_staticInputs.extraArgs     = {Ws, Wnames, spatialMatrices.effects};
end
if config.laggedBaseTransf.code == 2 || config.laggedBaseTransf.code == 4  || config.laggedBaseTransf.code == 5
	if strcmp(config.laggedBaseTransf.denom, 'Area')
		Xdynamic_staticInputs.extraArgs = {Ws, Wnames, spatialMatrices.effects,log(surfaces)};
	end
end

installedBases.laggedProviders  = laggedProviders;
installedBases.laggedConsumers = laggedConsumers;
installedBases.laggedInactiveUsers = laggedInactiveUsers;
Xparts_dynamic = make_Xdynamic_acctCreation(config, Xdynamic_staticInputs, installedBases, true);

%%%%% Make Xparts and dims %%%%%
dimType = 2;
Xparts = {};
dims1 = {};
Xpart_2_NumX = [];
Xpart_2_NumX_FEs = [];
Xpart_2_Num_FE_vals = {};
NumFEvals2Keep = {};

pidx = 0;

%%% T-K part:
pidx = pidx+1;
obj = Xparts_dynamic{1};
Xparts{pidx}              = obj;
dims1{pidx}               = NumSpells*K;
Xpart_2_NumX(pidx)        = size(obj.X,2);
Xpart_2_NumX_FEs(pidx)    = size(obj.X_FEs,2);
Xpart_2_Num_FE_vals{pidx} = obj.NumX_FE_vals;
NumFEvals2Keep{pidx}      = sum(obj.NumX_FE_vals) - length(obj.NumX_FE_vals);%

clear X obj;

%%% T part:
X      = myA(spell2period,:);            % NumSpells x NumX
Xnames = myAnames;                       % 1 x NumX
X_FEs        = time_FEs(spell2period,:); % NumSpells x NumX_FEs
X_FE_labels  = time_FE_labels;           % cell(1, NumX_FEs)
NumX_FE_vals = Num_time_FE_vals;         % 1 x NumX_FEs

pidx = pidx+1;
obj.X                     = X;
obj.Xnames                = Xnames;
obj.X_FEs                 = X_FEs;
obj.X_FE_labels           = X_FE_labels;
obj.NumX_FE_vals          = NumX_FE_vals;
Xparts{pidx}              = obj;
dims1{pidx}               = NumSpells;
Xpart_2_NumX(pidx)        = size(X,2);
Xpart_2_NumX_FEs(pidx)    = size(X_FEs,2);
Xpart_2_Num_FE_vals{pidx} = NumX_FE_vals;
NumFEvals2Keep{pidx}      = sum(NumX_FE_vals) - length(NumX_FE_vals);

clear X X_FEs NumX_FE_vals obj;

%%% K part:
X      = myZ;        % K x NumX
Xnames = myZ_names;  % 1 x NumX
X_FEs        = myZ_geo_FEs;  % K x NumX_FEs
X_FE_labels  = myZ_geo_FE_labels;   % cell(1, NumX_FEs)
NumX_FE_vals = myZ_Num_geo_FE_vals; % 1 x NumX_FEs

pidx = pidx+1;
obj.X                     = X;
obj.Xnames                = Xnames;
obj.X_FEs                 = X_FEs;
obj.X_FE_labels           = X_FE_labels;
obj.NumX_FE_vals          = NumX_FE_vals;
Xparts{pidx}              = obj;
dims1{pidx}               = K;
Xpart_2_NumX(pidx)        = size(X,2);
Xpart_2_NumX_FEs(pidx)    = size(X_FEs,2);
Xpart_2_Num_FE_vals{pidx} = NumX_FE_vals;
NumFEvals2Keep{pidx}      = sum(NumX_FE_vals) - length(NumX_FE_vals);

clear X X_FEs NumX_FE_vals obj;


%%%%% Make data object
% Calculate NumParams
NumParts = pidx;
NumParams = 0;
for ii = 1:NumParts
	NumParams = NumParams + size(Xparts{ii}.X,2);
	for ff = 1:size(Xparts{ii}.X_FEs,2)
		NumX_FE_vals_if = Xparts{ii}.NumX_FE_vals(ff);
		NumParams = NumParams + NumX_FE_vals_if - 1;
	end
end

% Make dims
dims.dimType  = dimType;
dims.NumParts = NumParts;
dims.NumObs = size(Y,1);
dims.NumParams      = NumParams;
dims.dims1               = dims1;
dims.Xpart_2_NumX        = Xpart_2_NumX;
dims.Xpart_2_NumX_FEs    = Xpart_2_NumX_FEs;
dims.Xpart_2_Num_FE_vals = Xpart_2_Num_FE_vals;
dims.NumFEvals2Keep      = NumFEvals2Keep;

% Make LL_offset
LL_offset = 0;

% Make data
clear data; % (Clear previous data object)
data.Y               = Y;                   % (NumSpells*K) x 1
data.Yname           = Yname;
data.logLambdaOffset = logLambdaOffset;     % (NumSpells*K) x 1
data.LL_offset       = LL_offset;           % scalar
data.Xparts          = Xparts;              % cell(3,1)
data.dims            = dims;                % cell(3,1)
data.periodLabels    = periodLabels;        % cell(NumPeriods,1)
data.geoLabels       = geoLabels;           % cell(K,1)
data.surfaces        = surfaces;            % K x 1
data.population      = population;          % K x 1
data.incomingCommuters = incomingCommuters; % K x 1
data.touristBeds       = touristBeds;       % K x 1
data.spellDurations  = spellDurations;      % NumSpells x 1
data.spell2period    = spell2period;        % NumSpells x 1 (values between 1 and NumPeriods)

% Save to output file
save(runDataFile, 'data', '-v7.3');

end